package com.todo.member.infrastructure.repository.impl;


import com.todo.member.infrastructure.convert.MemberBaseConvert;
import com.todo.member.infrastructure.entity.domain.MemberInfoDO;
import com.todo.member.infrastructure.entity.po.MemberInfoPO;
import com.todo.member.infrastructure.entity.po.MemberMedalInfoPO;
import com.todo.member.infrastructure.entity.po.MemberVipInfoPO;
import com.todo.member.infrastructure.mapper.MemberInfoMapper;
import com.todo.member.infrastructure.repository.MemberInfoRepository;
import com.todo.member.infrastructure.repository.MemberMedalInfoRepository;
import com.todo.member.infrastructure.repository.MemberVipInfoRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

/**
 * @author azhebuxing
 * @date 2025/2/10 11:19
 * @description
 */
@Slf4j
@Repository
public class MemberInfoRepositoryImpl implements MemberInfoRepository {

    @Resource
    private MemberInfoMapper memberInfoMapper;
    @Resource
    private MemberMedalInfoRepository memberMedalInfoRepository;
    @Resource
    private MemberVipInfoRepository memberVipInfoRepository;

    @Override
    public Boolean insertMemberInfo(Long userId) {
        if (userId == null) {
            return false;
        }
        MemberInfoPO memberInfoPO = MemberBaseConvert.buildInsertMemberInfo(userId);
        return memberInfoMapper.insertMemberInfo(memberInfoPO) > 0;
    }

    @Override
    public MemberInfoDO queryMemberInfoByUserId(Long userId) {
        if (userId == null) {
            return null;
        }
        MemberInfoPO memberInfoPO = memberInfoMapper.queryMemberInfoByUserId(userId);
        if (memberInfoPO == null) {
            return null;
        }
        MemberMedalInfoPO memberMedalInfoPO = null;
        MemberVipInfoPO memberVipInfoPO = null;
        if (memberInfoPO.getLevel() != null) {
            memberMedalInfoPO = memberMedalInfoRepository.queryMemberMedalInfoByMedalCode(memberInfoPO.getDisplayMedalCode());
        }
        if (memberInfoPO.getVipCode() != null) {
            memberVipInfoPO = memberVipInfoRepository.queryMemberVipInfoByVipCode(memberInfoPO.getVipCode(), memberInfoPO.getVipVersion());
        }
        return MemberBaseConvert.buildMemberInfoDO(memberInfoPO, memberMedalInfoPO, memberVipInfoPO);
    }

    @Override
    public Boolean removeMemberInfoByUserId(Long userId) {
        if (userId == null || userId < 0) {
            return false;
        }
        return memberInfoMapper.removeMemberInfoByUserId(userId) > 0;
    }

    @Override
    public Boolean updateMemberInfo(MemberInfoPO memberInfoPO) {
        if (memberInfoPO == null || memberInfoPO.getId() == null) {
            return false;
        }
        return memberInfoMapper.updateMemberInfo(memberInfoPO) > 0;
    }

    @Override
    public MemberInfoPO queryMemberInfoPOById(Long id) {
        return memberInfoMapper.queryMemberInfoPOById(id);
    }
}
